package cn.aitstone.service.controller;

import cn.aitstone.basic.exception.BusinessException;
import cn.aitstone.basic.utils.AjaxResult;
import cn.aitstone.basic.utils.LoginContext;
import cn.aitstone.basic.utils.PageList;
import cn.aitstone.org.domain.Employee;
import cn.aitstone.org.service.IEmployeeService;
import cn.aitstone.service.domain.Product;
import cn.aitstone.service.query.ProductQuery;
import cn.aitstone.service.service.IProductService;
import cn.aitstone.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    public IProductService productService;

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 保存和修改公用的
     * @param product  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Product product,HttpServletRequest request){
        try {
            Logininfo loginInfo = LoginContext.getLoginInfo(request);
            Employee employee = employeeService.loadByLogininfoId(loginInfo.getId());
            product.setShopId(employee.getShopId());
            if( product.getId()!=null)
                productService.update(product);
            else
                productService.save(product);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Product get(@PathVariable("id")Long id)
    {
        return productService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Product> list(){

        return productService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Product> json(@RequestBody ProductQuery query)
    {
        return productService.queryData(query);
    }


    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids, HttpServletRequest request){

        try {
            Logininfo loginInfo = LoginContext.getLoginInfo(request);
            Employee employee = employeeService.loadByLogininfoId(loginInfo.getId());
            Long eId = employee.getId();
            productService.onsale(ids, eId);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }
    }

    /**
     * @Title: offsale
     * @Description: 宠物下架接口
     * @Author: Sheldon
     * @Version: 1.0
     * @Date:  2022/7/16 20:06
     * @Parameters: [ids]
     * @Return cn.aitstone.basic.utils.AjaxResult
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            productService.offsale(ids);
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    /**
     * @Title: batch
     * @Description: 批量删除
     * @Author: Sheldon
     * @Version: 1.0
     * @Date:  2022/7/16 23:02
     * @Parameters: [ids]
     * @Return cn.aitstone.basic.utils.AjaxResult
     */
    @PatchMapping
    public AjaxResult batch(@RequestBody Long[] ids){
        try {
            productService.batchdel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请稍后重试");
        }

    }
}
